﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common;
using System.IO;
using net.veekee.common.reflection;

namespace net.veekee.logprocessor.plugin.input
{
    public class InputFactory
    {
        private static Dictionary<String, String> registeredInputs = new Dictionary<String, String>();

        public static String[] GetLoadedExtensions()
        {
            String[] array = new String[InputFactory.registeredInputs.Count];
            InputFactory.registeredInputs.Keys.CopyTo(array, 0);
            return array;
        }

        public static AParser Create(String format)
        {
            if (format == null)
                return null;
            format = format.ToLower();
            if (!InputFactory.registeredInputs.ContainsKey(format))
            {
                Log.GetInstance().Error("Extension '{0}' has not been registered", format);
                return null;
            }
            else
            {
                return ReflectionManager.CreateObject<AParser>(InputFactory.registeredInputs[format]);
            }
        }

        public static AParser CreateFromFileName(String fname)
        {
            if (fname == null)
                return null;
            FileInfo fi = new FileInfo(fname);
            return InputFactory.Create(fi.Extension);
        }

        public static void Register(String extension, String readerAssembly)
        {
            if (InputFactory.registeredInputs.ContainsKey(extension))
            {
                Log.GetInstance().Error("Couldn't register '{0}' to extension '{1}' ; Extension has already been registered with {2}", readerAssembly, extension, InputFactory.registeredInputs[extension]);
                return;
            }
            InputFactory.registeredInputs.Add(extension, readerAssembly);
        }
    }
}
